﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;
using Lottery.DataAccess;

public partial class FootBall_BJSingle_Calculation : System.Web.UI.Page
{
    string[] fullList = { "3", "1", "0" };

    string[][] unfullList = { 
                                new string[] { "3","0" },
                                new string[] { "3","1","0" },
                                new string[] { "3" },
                                new string[] { "1","0" },
                                new string[] { "3","1","0" }
                            };
    //const int testLength = 6; 
    List<string> resultSet = new List<string>();    
    Dictionary<string, int> dicResult = new Dictionary<string, int>();

    int testLength = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder output = new StringBuilder();
        Dictionary<string, int> dic = new Dictionary<string, int>();
        
        testLength = int.Parse(TextBox1.Text);        

        GetFullList(string.Empty);
        
        
        string result = ResultCollection.GetResultString();

        GetResultDictionary(result, testLength);

        foreach (string str in resultSet)
        {
            if (dicResult.ContainsKey(str))
            {
                dic.Add(str, dicResult[str]);
            }
            else
            {
                dic.Add(str, 0);
            }
        }

        //int count = int.Parse((dicResult.Count / 2).ToString());
        //List<KeyValuePair<string,int>> l = dicResult.OrderBy(k => k.Value).Take(count).ToList();

        foreach (KeyValuePair<string, int> k in dic.OrderBy(v => v.Value))
        {
            output.AppendFormat(k.Key.ToString() + "<br />");
        }

        Response.Write(output.ToString());
    }


    //full list
    private string GetFullList(string str)
    {

        if (str.Length == testLength)
        {
            resultSet.Add(str);
            return str;
        }
        else
        {
            for (int i = 0; i < fullList.Length; i++)
            {
                GetFullList(str + fullList[i]);
            }
            return string.Empty;
        }
    }

    //unfull list
    private string GetUnfullList(string str, int index)
    {
        if (str.Length == unfullList.Length)
        {
            resultSet.Add(str);
            return str;
        }
        else
        {
            for (int i = 0; i < unfullList[index].Length; i++)
            {
                GetUnfullList(str + unfullList[index][i], index + 1);
            }
            return string.Empty;
        }
    }

    private void GetResultDictionary(string result, int length)
    {
        string tempResult = result;

        while (tempResult.Length > length)
        {
            string str = tempResult.Substring(0, length);
            if (str.IndexOf("4") < 0)
            {
                int appearTime = Regex.Matches(result, str).Count;

                if (!dicResult.ContainsKey(str))
                {
                    dicResult.Add(str, appearTime);
                }
            }
            tempResult = tempResult.Remove(0, 1);
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {        
        StringBuilder output = new StringBuilder();

        GetUnfullList(string.Empty, 0);

        string result = ResultCollection.GetResultString();
        GetResultDictionary(result, unfullList.Length);


        Dictionary<string, int> dic = new Dictionary<string, int>();

        foreach (string s in resultSet)
        {
            dic.Add(s, dicResult[s]);            
        }

        foreach (KeyValuePair<string, int> k in dic.OrderBy(d => d.Value))
        {
            output.AppendFormat(k.Key + " -- " + k.Value + "<br />");
        }

        Response.Write(output.ToString());
    }
}
